Ext.define('Zm.view.model.AttrList',{
    alias:'widget.attrlist',
    extend:'Ext.grid.Panel',
    uses:[
        'Zm.i18n.Lang',
        'Ext.grid.RowNumberer',
        'Zm.model.Attr',
        'Ext.ux.TreePicker',
        'Ext.grid.plugin.CellEditing'
    ],
    
    initComponent: function(){
        var me = this,
            cm = Zm.Common,
            lang = Zm.i18n.Lang;
        me.cellEditing = Ext.create('Ext.grid.plugin.CellEditing',{errorSummary:false});
        me.store = Ext.create('Ext.data.Store',{model:'Zm.model.Attr'});
        me.typeStore = Ext.StoreManager.get('TypeDef');
        me.editors = new Ext.util.HashMap();
        Ext.apply(me,{
            multiSelect:true,
            selModel:{
                listeners:{
                    selectionchange:{
                        fn:me.onSelChange,
                        scope:me
                    }
                }
            },
            plugins: [me.cellEditing],
            columns:[
                {xtype:'rownumberer',text:lang.tIndex,width:30},
                {text:lang.tAttrName,width:140,dataIndex:'attrname',editor:'textfield'},
                {
                    text:lang.tVType,
                    dataIndex:'vtid',
                    renderer:me.rendVType,
                    editor:{
                        xtype:'treepicker',
                        store: me.typeStore,
                        rootVisible:false,
                        displayField:'typename',
                        matchFieldWidth:false,
                        listWidth:300
                    }
                },
                {text:lang.tDefVal,dataIndex:'defval',renderer:me.rendVal,getEditor:me.getCellEditor},
                {text:lang.tMemo,dataIndex:'memo',flex:1,editor:'textfield'},
                {text:lang.tIsVisible,dataIndex:'isvisible',editor:'checkbox'}
            ],
            dockedItems: [{
                xtype:'toolbar',
                dock:'top',
                items:[{
                    text:lang.tAdd,
                    iconCls:'attradd',
                    handler:me.addAttr,
                    scope:me
                },{
                    text:lang.tDel,
                    iconCls:'attrdel',
                    itemId:'attrdel',
                    disabled: true,
                    handler:me.delAttr,
                    scope:me
                }]
            }],
            listeners:{
                edit:me.onEdit
            }
        });
        me.callParent(arguments);
    },
    onSelChange: function(selModel, selections){
        var me = this,
            flag = selections.length === 0,
            btn2 = me.down('#attrdel');
        btn2.setDisabled(flag);
    },
    rendVType:function(val){
        var store = Ext.StoreManager.get('TypeDef'),
            node = store.getNodeById(val);
        if(node){
            return node.data.typename;
        }
        return val;
    },
    addAttr:function(){
        var me = this,
            rd = Ext.create('Zm.model.Attr');
        me.store.add(rd);
    },
    delAttr:function(){
        var me = this,
            lang = Zm.i18n.Lang,
            sm = me.getSelectionModel(),
            records = sm.getSelection();
        if(records.length>0){
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg:lang.tDelAttrTip,
                   buttons: Ext.Msg.YESNO,
                   fn: function(btn){
                       if(btn=='yes') me.store.remove(records);
                   },
                   icon: Ext.Msg.QUESTION
               });                        
        }
    },
    
    getCellEditor:function(record){
        var me = this.up('attrlist'),
            store = Ext.StoreManager.get('TypeDef'),
            vtid = record.get('vtid'),
            node = store.getNodeById(vtid),
            typedata,
            editor = null;
        if(node){
            typedata = node.data;
            editor = me.editors.get(vtid);
            if(editor === undefined){
                editor = Zm.Common.getEditor(typedata);
                me.editors.add(vtid,editor);
            }
        }
        return editor;
    },
    beforeDestroy: function() {
        var me = this,editor;
        me.callParent();
        for(editor in me.editors){
            Ext.destroy(editor);
        }
    },
    getValue: function(){
        var me = this,
            details = me.store.getRange(),
            ret = new Array(),
            i,
            n=0;
        me.cellEditing.completeEdit();
        for(i=0;i<details.length;i++){
            ret[n++] = details[i].data;
        }
        return ret;
    },
    setValue:function(ddid){
        var me = this;
        Ext.Ajax.request({
            url: g_app + '/DevDef/getAttr',
            params: {ddid: ddid},
            scope: me,
            success:function(response){
                var obj = Ext.decode(response.responseText,true);
                if(obj==null) return;
                if(obj.success){
                    me.store.loadData(obj.data);
                }
            },
            failure:Zm.Common.onAFail
        });
    },
    rendVal:function(val,meta,record,rowIndex,colIndex,store,view){
        return record.get('valdesc');
    },
    onEdit:function(editor,e,eOpt){
        var value = editor.getRawValue();
        if(e.colIdx==3){
            record.set('valdesc',value);
        }
    }
});